﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ECSE6770.Models;

namespace ECSE6770.Controllers
{
    [Authorize(Roles = "Admin")]
    public class VenueController : BaseAdminController
    {
        //
        // GET: /Venue/

        public ActionResult Index()
        {
            return View(Db.Venues.OrderBy(e => e.Name).ToList());
        }

        public ActionResult New()
        {
            return View("Edit", null);
        }

        public ActionResult Edit(int id)
        {
            return View(Db.Venues.First(v => v.Id == id));
        }

        [HttpPost]
        public ActionResult Edit(Venue model)
        {
            if (ModelState.IsValid)
            {
                if (model.Id > 0)
                {
                    //updating a venue
                    Db.Entry(model).State = System.Data.EntityState.Modified;
                    Db.SaveChanges();
                    return Success("Venue Updated", "Your venue has been successfully updated.", "View All Venues", Url.Action("Index", "Venue"));
                }
                else
                {
                    //inserting a new venue
                    Db.Venues.Add(model);
                    Db.SaveChanges();
                    return Success("Venue Added", "Your venue has been successfully added.", "View All Venues", Url.Action("Index", "Venue"));
                }
            }
            else
                throw new NotImplementedException("Need to handle the invalid case gracefully.");
        }

        public ActionResult ConfirmDelete(int id)
        {
            return View(Db.Venues.FirstOrDefault(v => v.Id == id));
        }

        public ActionResult Delete(int id)
        {
            Venue venue = Db.Venues.FirstOrDefault(v => v.Id == id);
            Db.Venues.Remove(venue);
            Db.SaveChanges();
            return View(venue);
        }
    }
}
